package com.amazon.web.controller;

import com.amazon.bo.BrowseTree;
import com.amazon.bo.Product;
import com.amazon.service.I18nService;
import com.amazon.service.BrowseTreeService;
import com.amazon.web.util.ApiResponse;
import com.amazon.web.util.OSSManageUtil;
import com.amazon.web.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value="browsetree")
public class BrowseTreeController {
    @Value("${is.open.cms}")
    public String isOpenCms;


    private static final String APP_ID = "20180715000185622";
    private static final String SECURITY_KEY = "RtGMlsmDQUDfmQ6Ddi5L";

    @Resource
    private OSSManageUtil ossManageUtil;

    @Resource
    BrowseTreeService browseTreeService;

    @Autowired
    I18nService i18nService;

    @RequestMapping(value="getBrowseTreeList",method = RequestMethod.GET)
    public ApiResponse getBrowseTreeList(String search, int page, int pagesize) {
        StringBuilder conn=new StringBuilder("");
        //条件
        if(!search.equals("")){
            conn.append(" (node_root LIKE '%"+search+"%' OR path LIKE '%"+search+"%' or ch_path like '%"+search+"%' or en_id like '%"+search+"%') ");
        }

        List<BrowseTree> list=browseTreeService.getBrowseTreeList(conn.length()>0?" where "+conn.toString():"",page,pagesize);

        int total = browseTreeService.getBrowseTreeCount(conn.length()==0?"": conn.toString());
        return ResponseUtil.getSuccessResponse(list, total);
    }

    @RequestMapping(value = "chPathSave", method = RequestMethod.POST)
    public Object chPathSave(BrowseTree k){
        boolean flag =browseTreeService.update(k);
        return flag;
    }
    @RequestMapping(value = "getAmazonInfoByEnid", method = RequestMethod.GET)
    public ApiResponse getAmazonInfoByEnid(String enid){
        BrowseTree bt=browseTreeService.getAmazonInfoByEnid(enid);
        return ResponseUtil.getSuccessResponse(bt);
    }

}
